package com.dst.smart.dao.impl.factory;

import java.util.ArrayList;
import java.util.List;

import com.dst.smart.app.MyApplication;
import com.dst.smart.model.Mode;

public class ModeFactory {
	public static List<Mode> list = new ArrayList<Mode>();

	static {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}
	}

	public static List<Mode> getAll() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}
		return list;
	}

	public static Mode getModeById(int id) {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}
		Mode result = null;
		for (Mode mode : list) {
			if (mode.getId() == id) {
				result = mode;
				break;
			}
		}
		return result;
	}

	public static void clearList() {
		list = MyApplication.getInstance().getWidgetDataBase().getMode();
	}

	public static int judgeName(String modeName) {
		int result = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}
		for (Mode mode : list) {
			String name = mode.getModeName();
			if (name.indexOf(modeName) != -1 || modeName.indexOf(name) != -1) {
				result = 1;
				break;
			}
		}
		return result;
	}
	public static int updateName(int id, String deviceName, String oldName) {
		int result = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}
		if (deviceName.equals(oldName)) {
			result = 0;
			return result;
		}

		int num = 0;
		for (Mode device : list) {
			String name = device.getModeName();
			if (name.indexOf(deviceName) != -1
					|| deviceName.indexOf(name) != -1) {
				if (device.getId() == id) {
					result = 0;
				} else {
					num++;
				}
				if (num > 0) {
					result = 1;
					break;
				}
			}
		}
		return result;
	}

	public static List<Mode> getLimit(int limit) {

		List<Mode> resultList = new ArrayList<Mode>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getMode();
		}

		if (limit < list.size()) {
			resultList = list.subList(0, limit);
		} else {
			resultList.addAll(list);
		}
		return resultList;
	}
}
